package com.sinosoft.tcmemr.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.framework.core.common.exception.ObjectIsNotExistedException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.security.DigestUtil;
import com.sinosoft.framework.core.common.utils.security.EncodeUtil;
import com.sinosoft.tcmemr.entity.DoctorUserEntity;
import com.sinosoft.tcmemr.manager.DoctorUserManager;

@Service
public class DoctorUserService {
	@Autowired
	private DoctorUserManager doctorUserManager;
	
	private Logger logger = LoggerFactory.getLogger(DoctorUserService.class);
	
	public static final String HASH_ALGORITHM = "SHA-1";	//	加密算法
	public static final int HASH_INTERATIONS = 1024;	//	随机盐生成函数中，遍历大小值
	private static final int SALT_SIZE = 8;	//	盐长度
	
	/**
	 * 通过用户名得到医生用户信息
	 * @param userName
	 * @return DoctorUserEntity
	 */
	public DoctorUserEntity getDoctorByUserName(String userName){
		return doctorUserManager.getDoctorByUserName(userName);
	}
	/**
	 * 通过ID得到医生用户信息
	 * @param id
	 * @return DoctorUserEntity
	 */
	public DoctorUserEntity getDoctorByPrimaryKey(Integer id){
		return doctorUserManager.getDoctorByPrimaryKey(id);
	}
	/**
	 * 添加医生用户信息
	 * @param doctoruser
	 * @return true/false
	 */
	public boolean addDoctorUser(DoctorUserEntity doctoruser){
		if ( doctoruser == null ){
			return false;
		}
		doctorUserManager.addDoctorUser(doctoruser);
		return true;
	}
	/**
	 * 更新医生用户信息
	 * @param doctoruser
	 * @return true/false
	 */
	public boolean updateDoctorUser(DoctorUserEntity doctoruser){
		if ( doctoruser == null ){
			return false;
		}
		doctorUserManager.updateDoctorUser(doctoruser);
		return true;
	}
	/**
	 * 删除医生用户信息
	 * @param id 用户ID
	 * @return true/false
	 */
	public boolean deleteByPrimaryKey(Integer id){
		doctorUserManager.deleteByPrimaryKey(id);
		return true;
	}
	
	/**
	 * 校验用户密码
	 * 传入的密码为原文
	 * 若username或password为空，抛出异常消息“DoctorUserService:checkUserPassword调用失败，username或password为空”
	 */
	public boolean checkUserPassword(String aid, String username, String password)
			throws ObjectIsNotExistedException {
		if(StringUtil.isBlank(aid) || StringUtil.isBlank(username) || StringUtil.isBlank(password)) {
			logger.error(("DoctorUserService:checkUserPassword调用失败，参数为空"));
			return false;
		}
		return comparePassword(password, doctorUserManager.getDoctorByUserName(username) );
	}
	
	/**
	 * 密码比较
	 * 比较传入的密码原文与用户信息中加密后的密码是否匹配
	 * @param password	需要比较的密码
	 * @param userEntiy	用户信息
	 * @return	true/false
	 */
	private boolean comparePassword(String password, DoctorUserEntity doctoruser) {
		if(doctoruser == null) {
			return false;
		}
		byte[] salt = EncodeUtil.decodeHex(doctoruser.getSalt());
		byte[] hashPassword = DigestUtil.sha1(password.getBytes(), salt, HASH_INTERATIONS);
		if(doctoruser.getPassword().equals(EncodeUtil.encodeHex(hashPassword))){
			return true;
		}else{
			return false;
		}
	}
	
}
